package tr.richfacesext.examples;

import java.io.IOException;
import java.io.InputStream;

/**
 * 
 * @author mert
 *
 */
public class ImgViewerController {
	
	public byte[] getPic1() {
		return getPic("simpsons-star-wars.jpg");
	}
	
	public byte[] getPic(String fileName) {
		return getBytesFromFile(this.getClass().getResourceAsStream(fileName));
	}
	
	public byte[] getBytesFromFile(InputStream inputStream) {
		int indice, tempIndice;
		
		byte tempArr[];
		byte mainArr[] = new byte[0];
		byte byteArr[] = new byte[65535];
		
        try {
        	for (indice = 0; (indice = inputStream.read(byteArr)) > 0;) {
				tempIndice = mainArr.length + indice;
				tempArr = new byte[tempIndice];
				System.arraycopy(mainArr, 0, tempArr, 0, mainArr.length);
				System.arraycopy(byteArr, 0, tempArr, mainArr.length, indice);
				mainArr = tempArr;
			}
		} 
        catch (IOException e) {
			e.printStackTrace();
		}
    
        return mainArr;
    }
}
